package com.chuangmei.com.arindoorlocationsystem.config.fragment;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chuangmei.com.arindoorlocationsystem.R;
import com.chuangmei.com.arindoorlocationsystem.config.adapter.AppMessageAdapter;
import com.chuangmei.com.arindoorlocationsystem.base.BaseFragment;
import com.chuangmei.com.arindoorlocationsystem.bean.AppInfo;
import com.chuangmei.com.arindoorlocationsystem.bean.MapItem;
import com.chuangmei.com.arindoorlocationsystem.config.ConfigMessageActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.AppInfoUtils;
import com.chuangmei.com.arindoorlocationsystem.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：ARIndoorLocationSystem
 * 类描述：应用信息展示Fragment
 * 创建人：LuoHao
 * 创建时间：2018/4/8 17:41
 * 修改人：LuoHao
 * 修改时间：2018/4/8 17:41
 * 修改备注：
 */

public class AppMessageFragment extends BaseFragment {
    //    @BindView(R.id.mTV_app_message)
    //    TextView mTV_app_message;

    @Override
    protected int setLayout() {
        return R.layout.fragment_app_message;
    }

    @Override
    protected void initView(View view) {
        RecyclerView mRV_app_message = (RecyclerView) view.findViewById(R.id.mRV_app_message);
        mRV_app_message.setLayoutManager(new LinearLayoutManager(getContext()));

        ArrayList<MapItem> appMessages = new ArrayList<>();
        String app_name = SharedPreferencesUtils.getUserAppMessage("app_name");
        String version_name;
        String lastUpdateTime;
        if (TextUtils.isEmpty(app_name)) {
            List<AppInfo> appInfos = AppInfoUtils.getAppInfos(getContext());
            AppInfo appInfo = appInfos.get(0);
            app_name = appInfo.getName();
            SharedPreferencesUtils.setUserAppMessage("app_name", app_name);
            version_name = appInfo.getVersionName();
            SharedPreferencesUtils.setUserAppMessage("version_name", version_name);
            lastUpdateTime = appInfo.getLastUpdateTime();
            SharedPreferencesUtils.setUserAppMessage("lastUpdateTime", lastUpdateTime);
        } else {
            app_name = SharedPreferencesUtils.getUserAppMessage("app_name");
            version_name = SharedPreferencesUtils.getUserAppMessage("version_name");
            lastUpdateTime = SharedPreferencesUtils.getUserAppMessage("lastUpdateTime");
        }
        MapItem mapItem1 = new MapItem("应用名：", app_name);
        appMessages.add(mapItem1);
        MapItem mapItem2 = new MapItem("版本号：", version_name);
        appMessages.add(mapItem2);
        MapItem mapItem3 = new MapItem("安装时间：", lastUpdateTime);
        appMessages.add(mapItem3);

        AppMessageAdapter appMessageAdapter = new AppMessageAdapter(R.layout.item_app_message_layout, appMessages);
        mRV_app_message.setAdapter(appMessageAdapter);
        //添加Android自带的分割线
        mRV_app_message.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

    }

    @Override
    public void onResume() {
        super.onResume();
        ((ConfigMessageActivity) getActivity()).chengTitle(getResources().getString(R.string.app_message_title));
    }

    @Override
    public void onPause() {
        super.onPause();
        ((ConfigMessageActivity) getActivity()).chengTitle(getResources().getString(R.string.config_title));
    }
}
